package com.ruoyi.project.modules.parameter.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.frontInterfaceFieldSource.mapper.FrontInterfaceFieldSourceMapper;
import com.ruoyi.project.modules.interfaceObject.service.IInterfaceObjectService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.mapper.InterfaceInfoMapper;
import com.ruoyi.project.modules.interfaceinfo.request.BffParamReq;
import com.ruoyi.project.modules.interfaceinfo.request.VirtualUsageParamReq;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.mapper.InterfaceParamsFieldRelationMapper;
import com.ruoyi.project.modules.processBranch.service.IProcessBranchService;
import com.ruoyi.project.modules.processBranchCondition.domain.ProcessBranchCondition;
import com.ruoyi.project.modules.processBranchCondition.service.IProcessBranchConditionService;
import com.ruoyi.project.modules.processBranchConditionDetail.domain.ProcessBranchConditionDetail;
import com.ruoyi.project.modules.processBranchConditionDetail.service.IProcessBranchConditionDetailService;
import com.ruoyi.project.modules.table.domain.FieldInfo;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.virtualUsageFieldMerge.mapper.VirtualUsageFieldMergeMapper;
import com.ruoyi.project.modules.virtualUsageGroup.domain.VirtualUsageGroup;
import com.ruoyi.project.modules.virtualUsageGroup.service.IVirtualUsageGroupService;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.mapper.VirtualUsageParamSourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.parameter.mapper.ParamsFieldRelationMapper;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 数据集下面字段关系Service业务层处理
 *
 * @author smallrain
 * @date 2021-10-30
 */
@Service
public class ParamsFieldRelationServiceImpl implements IParamsFieldRelationService {
    @Resource
    private ParamsFieldRelationMapper paramsFieldRelationMapper;

    /**
     * 查询数据集下面字段关系
     *
     * @param id 数据集下面字段关系主键
     * @return 数据集下面字段关系
     */
    @Override
    public ParamsFieldRelation selectParamsFieldRelationById(Long id) {
        return paramsFieldRelationMapper.selectParamsFieldRelationById(id);
    }

    /**
     * 查询数据集下面字段关系列表
     *
     * @param paramsFieldRelation 数据集下面字段关系
     * @return 数据集下面字段关系
     */
    @Override
    public List<ParamsFieldRelation> selectParamsFieldRelationList(ParamsFieldRelation paramsFieldRelation) {
        return paramsFieldRelationMapper.selectParamsFieldRelationList(paramsFieldRelation);
    }

    @Resource
    private InterfaceParamsFieldRelationMapper interfaceParamsFieldRelationMapper;
    @Resource
    private IInterfaceObjectService interfaceObjectService;

    /**
     * 新增数据集下面字段关系
     *
     * @param paramsFieldRelation 数据集下面字段关系
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertParamsFieldRelation(ParamsFieldRelation paramsFieldRelation) {
        paramsFieldRelation.setCreateTime(DateUtils.getNowDate());
//        InterfaceParamsFieldRelation interfaceParamsFieldRelation = interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(paramsFieldRelation.getInterfaceDataListId());
        //this.interfaceObjectService.insertInterfaceObject(paramsFieldRelation.getTableId(), interfaceParamsFieldRelation.getInterfaceId(), "1");
        return paramsFieldRelationMapper.insertParamsFieldRelation(paramsFieldRelation);
    }

    /**
     * 修改数据集下面字段关系
     *
     * @param paramsFieldRelation 数据集下面字段关系
     * @return 结果
     */
    @Override
    public int updateParamsFieldRelation(ParamsFieldRelation paramsFieldRelation) {
        paramsFieldRelation.setUpdateTime(DateUtils.getNowDate());
        return paramsFieldRelationMapper.updateParamsFieldRelation(paramsFieldRelation);
    }

    /**
     * 批量删除数据集下面字段关系
     *
     * @param ids 需要删除的数据集下面字段关系主键
     * @return 结果
     */
    @Override
    public int deleteParamsFieldRelationByIds(String ids) {
        return paramsFieldRelationMapper.deleteParamsFieldRelationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除数据集下面字段关系信息
     *
     * @param id 数据集下面字段关系主键
     * @return 结果
     */
    @Override
    public int deleteParamsFieldRelationById(Long id) {
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        List<ParamsFieldRelation> paramsFieldRelationList = paramsFieldRelationMapper.selectParamsFieldRelationByDataRelationId(Arrays.asList(id), null);
        if (CollectionUtil.isNotEmpty(paramsFieldRelationList)) {
            for (ParamsFieldRelation paramsFieldRelation : paramsFieldRelationList) {
                ids.add(paramsFieldRelation.getId());
            }
        }
        paramsFieldRelationMapper.deleteParamsFieldRelationByDataRelationId(ids);
        return paramsFieldRelationMapper.deleteParamsFieldRelationById(id);
    }

    @Autowired
    private ITableInfoService tableInfoService;

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;

    @Override
    public void saveParamsField(ParamsFieldRelation paramsFieldRelation) {
        int maxSort = this.paramsFieldRelationMapper.maxSort(paramsFieldRelation.getInterfaceDataListId()) + 1;
        List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
        List<FieldInfo> fieldInfoList = this.tableInfoService.selectFieldInfoByFieldId(paramsFieldRelation.getFieldIds());
        Map<Long, List<FieldInfo>> fieldInfoMap = fieldInfoList.stream().collect(Collectors.groupingBy(FieldInfo::getId));
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(paramsFieldRelation.getInterfaceDataListId());
        boolean flag = false;
        if (interfaceParamsFieldRelation.getType().equals("1")) {
            InterfaceInfo interfaceInfo = this.interfaceInfoMapper.selectInterfaceInfoById(interfaceParamsFieldRelation.getInterfaceId());
            if (interfaceInfo.getOptType().equals("1") || interfaceInfo.getOptType().equals("2")) {
                flag = true;
            }
        }
        for (Long fieldId : paramsFieldRelation.getFieldIds()) {
            ParamsFieldRelation pr = new ParamsFieldRelation();
            pr.setFieldId(fieldId);
            pr.setInterfaceDataListId(paramsFieldRelation.getInterfaceDataListId());
            pr.setCreateBy(ShiroUtils.getLoginName());
            pr.setCreateTime(new Date());
            pr.setSortNum(maxSort ++);
            List<FieldInfo> fieldInfos = fieldInfoMap.get(fieldId);
            pr.setFieldDescription(fieldInfos.get(0).getFieldDescription());
            pr.setDesignContent(fieldInfos.get(0).getDesignContent());
            if (flag) {
                pr.setIsRequired(fieldInfos.get(0).getIsRequired());
                pr.setIsRequiredParent(fieldInfos.get(0).getIsRequired());
            }
            if (StringUtils.isNotEmpty(fieldInfos.get(0).getDesignContent())) {
                pr.setIsUpdate("N");
            } else {
                pr.setIsUpdate("Y");
            }
            paramsFieldRelationList.add(pr);
        }
        this.paramsFieldRelationMapper.batchSave(paramsFieldRelationList);
    }

    @Override
    public List<TableInfo> tableList(Long interfaceDataListId) {
        return paramsFieldRelationMapper.tableList(interfaceDataListId);
    }

    @Resource
    private VirtualUsageParamSourceMapper virtualUsageParamSourceMapper;

    @Override
    public void saveVirtualUsageParam(VirtualUsageParamReq virtualUsageParamReq) {
        ParamsFieldRelation relation = new ParamsFieldRelation();
        relation.setIds(virtualUsageParamReq.getId());
        List<ParamsFieldRelation> fieldRelationList = this.paramsFieldRelationMapper.selectParamsFieldRelationList(relation);
        for (ParamsFieldRelation paramsFieldRelation : fieldRelationList) {
            paramsFieldRelation.setInterfaceDataListId(virtualUsageParamReq.getInterfaceParamsFieldRelationId());
            paramsFieldRelation.setDataRelationId(paramsFieldRelation.getId());
            paramsFieldRelation.setIsRequiredParent(paramsFieldRelation.getIsRequired());
            paramsFieldRelation.setDataRelationType("2");
            if (StringUtils.isNotEmpty(paramsFieldRelation.getDesignContent())) {
                paramsFieldRelation.setIsUpdate("N");
            }
            paramsFieldRelation.setId(null);
        }
        this.paramsFieldRelationMapper.batchSave(fieldRelationList);
    }

    @Override
    public void saveBffParam(BffParamReq bffParamReq, List<Long> interfaceParamsFieldRelationIds, List<Long> paramsFieldRelationIds) {
        int maxSort = this.paramsFieldRelationMapper.maxSort(bffParamReq.getInterfaceParamsFieldRelationId()) + 1;
        List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(paramsFieldRelationIds)) {
            ParamsFieldRelation relation = new ParamsFieldRelation();
            relation.setIds(paramsFieldRelationIds);
            List<ParamsFieldRelation> fieldRelationList = this.paramsFieldRelationMapper.selectParamsFieldRelations(relation);
            for (ParamsFieldRelation paramsFieldRelation : fieldRelationList) {
                paramsFieldRelation.setInterfaceDataListId(bffParamReq.getInterfaceParamsFieldRelationId());
                paramsFieldRelation.setIsRequiredParent(paramsFieldRelation.getIsRequired());
                paramsFieldRelation.setDataRelationId(paramsFieldRelation.getId());
                paramsFieldRelation.setDataRelationType("2");
                paramsFieldRelation.setId(null);
                paramsFieldRelationList.add(paramsFieldRelation);
            }
        }

        if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelationIds)) {
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
            interfaceParamsFieldRelation.setIds(interfaceParamsFieldRelationIds);
            List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList2 = interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
            for (InterfaceParamsFieldRelation interfaceParamsFieldRelation1 : interfaceParamsFieldRelationList2) {
                ParamsFieldRelation paramsFieldRelation = new ParamsFieldRelation();
                paramsFieldRelation.setDataRelationId(interfaceParamsFieldRelation1.getId());
                paramsFieldRelation.setIsRequiredParent(interfaceParamsFieldRelation1.getIsRequired());
                paramsFieldRelation.setFieldId(interfaceParamsFieldRelation1.getObjectId());
                paramsFieldRelation.setSortNum(maxSort ++);
                paramsFieldRelation.setDataRelationType("1");
                paramsFieldRelation.setInterfaceId(interfaceParamsFieldRelation1.getInterfaceId());
                paramsFieldRelation.setInterfaceDataListId(bffParamReq.getInterfaceParamsFieldRelationId());
                paramsFieldRelation.setAliasZhName(interfaceParamsFieldRelation1.getAliasZhName());
                paramsFieldRelation.setAliasName(interfaceParamsFieldRelation1.getAliasName());
                paramsFieldRelation.setDictCategory(interfaceParamsFieldRelation1.getDictCategory());
                paramsFieldRelation.setDictType(interfaceParamsFieldRelation1.getDictType());
                paramsFieldRelation.setDictValue(interfaceParamsFieldRelation1.getDictValue());
                paramsFieldRelationList.add(paramsFieldRelation);
            }
        }
        this.paramsFieldRelationMapper.batchSave(paramsFieldRelationList);
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(bffParamReq.getInterfaceParamsFieldRelationId());
        if (interfaceParamsFieldRelation.getIsInParam().equals("1")) {
            List<VirtualUsageParamSource> virtualUsageParamSourceList = new ArrayList<>();
            for (ParamsFieldRelation paramsFieldRelation : paramsFieldRelationList) {
                VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
                virtualUsageParamSource.setObjectId(paramsFieldRelation.getDataRelationId());
                virtualUsageParamSource.setType(paramsFieldRelation.getDataRelationType());
                virtualUsageParamSource.setSourceobjectId(paramsFieldRelation.getId());
                virtualUsageParamSource.setSourceType("2");
                virtualUsageParamSource.setInterfaceId(paramsFieldRelation.getInterfaceId());
                int i = this.virtualUsageParamSourceMapper.update(virtualUsageParamSource);
                if (i == 0) {
                    this.virtualUsageParamSourceMapper.insertVirtualUsageParamSource(virtualUsageParamSource);
                }
            }
        }
    }

    @Override
    public void saveDubboParam(BffParamReq bffParamReq, List<Long> interfaceParamsFieldRelationIds, List<Long> paramsFieldRelationIds) {
        int maxSort = this.paramsFieldRelationMapper.maxSort(bffParamReq.getInterfaceParamsFieldRelationId()) + 1;
        List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(paramsFieldRelationIds)) {
            ParamsFieldRelation relation = new ParamsFieldRelation();
            relation.setIds(paramsFieldRelationIds);
            List<ParamsFieldRelation> fieldRelationList = this.paramsFieldRelationMapper.selectParamsFieldRelations(relation);
            for (ParamsFieldRelation paramsFieldRelation : fieldRelationList) {
                paramsFieldRelation.setInterfaceDataListId(bffParamReq.getInterfaceParamsFieldRelationId());
                paramsFieldRelation.setDataRelationId(paramsFieldRelation.getId());
                paramsFieldRelation.setIsRequiredParent(paramsFieldRelation.getIsRequired());
                paramsFieldRelation.setDataRelationType("2");
                paramsFieldRelation.setId(null);
                if (StringUtils.isNotEmpty(paramsFieldRelation.getDesignContent())) {
                    paramsFieldRelation.setIsUpdate("N");
                }
                paramsFieldRelationList.add(paramsFieldRelation);
            }
        }

        if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelationIds)) {
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
            interfaceParamsFieldRelation.setIds(interfaceParamsFieldRelationIds);
            List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList2 = interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
            for (InterfaceParamsFieldRelation interfaceParamsFieldRelation1 : interfaceParamsFieldRelationList2) {
                ParamsFieldRelation paramsFieldRelation = new ParamsFieldRelation();
                paramsFieldRelation.setDataRelationId(interfaceParamsFieldRelation1.getId());
                paramsFieldRelation.setFieldId(interfaceParamsFieldRelation1.getObjectId());
                paramsFieldRelation.setIsRequiredParent(interfaceParamsFieldRelation1.getIsRequired());
                paramsFieldRelation.setSortNum(maxSort ++);
                paramsFieldRelation.setDataRelationType("1");
                paramsFieldRelation.setInterfaceId(interfaceParamsFieldRelation1.getInterfaceId());
                paramsFieldRelation.setInterfaceDataListId(bffParamReq.getInterfaceParamsFieldRelationId());
                paramsFieldRelation.setFieldDescription(interfaceParamsFieldRelation1.getFieldDescription());
                paramsFieldRelation.setDesignContent(interfaceParamsFieldRelation1.getDesignContent());
                paramsFieldRelation.setOrderType(interfaceParamsFieldRelation1.getOrderType());
                paramsFieldRelation.setIsRequired(interfaceParamsFieldRelation1.getIsRequired());
                paramsFieldRelation.setAliasZhName(interfaceParamsFieldRelation1.getAliasZhName());
                paramsFieldRelation.setAliasName(interfaceParamsFieldRelation1.getAliasName());
                paramsFieldRelation.setDictCategory(interfaceParamsFieldRelation1.getDictCategory());
                paramsFieldRelation.setDictType(interfaceParamsFieldRelation1.getDictType());
                paramsFieldRelation.setDictValue(interfaceParamsFieldRelation1.getDictValue());
                if (StringUtils.isNotEmpty(interfaceParamsFieldRelation1.getDesignContent())) {
                    paramsFieldRelation.setIsUpdate("N");
                }
                paramsFieldRelationList.add(paramsFieldRelation);
            }
        }
        this.paramsFieldRelationMapper.batchSave(paramsFieldRelationList);
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(bffParamReq.getInterfaceParamsFieldRelationId());
        if (interfaceParamsFieldRelation.getIsInParam().equals("1")) {
            List<VirtualUsageParamSource> virtualUsageParamSourceList = new ArrayList<>();
            for (ParamsFieldRelation paramsFieldRelation : paramsFieldRelationList) {
                VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
                virtualUsageParamSource.setObjectId(paramsFieldRelation.getDataRelationId());
                virtualUsageParamSource.setType(paramsFieldRelation.getDataRelationType());
                virtualUsageParamSource.setSourceobjectId(paramsFieldRelation.getId());
                virtualUsageParamSource.setSourceType("2");
                virtualUsageParamSource.setInterfaceId(paramsFieldRelation.getInterfaceId());
                int i = this.virtualUsageParamSourceMapper.update(virtualUsageParamSource);
                if (i == 0) {
                    this.virtualUsageParamSourceMapper.insertVirtualUsageParamSource(virtualUsageParamSource);
                }
            }
        }
    }


    @Resource
    private FrontInterfaceFieldSourceMapper frontInterfaceFieldSourceMapper;

    @Resource
    private VirtualUsageFieldMergeMapper virtualUsageFieldMergeMapper;

    @Override
    @Transactional
    public void removeParamsField(Long id, InterfaceParamsFieldRelation interfaceParamsFieldRelation) {
        if (interfaceParamsFieldRelation != null) {
            boolean isBranch = false;
            String type = interfaceParamsFieldRelation.getType();
            Long interfaceId = interfaceParamsFieldRelation.getInterfaceId();
            if ("5".equals(type)) {
                isBranch = true;
            } else {
                if ("3".equals(type)) {
                    VirtualUsage virtualUsage1 = this.virtualUsageService.selectVirtualUsageById(interfaceId);
                    interfaceId = virtualUsage1.getRelationInterfaceId();
                }
                InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(interfaceId);
                isBranch = "Y".equals(interfaceInfo.getIsBranch());
            }
            this.checkFieldSource(Arrays.asList(id), "2", isBranch);
            this.checkProcess(Arrays.asList(id), "2", isBranch);
        }
        this.paramsFieldRelationMapper.deleteParamsFieldRelationById(id);
        this.virtualUsageParamSourceMapper.deleteByObjectId(Arrays.asList(id), "2");
        if (interfaceParamsFieldRelation !=null && "2".equals(interfaceParamsFieldRelation.getType())) {
            // 删除前端字段来源
            this.frontInterfaceFieldSourceMapper.deleteBySourceObjectIdAndSourceObjectFieldId(interfaceParamsFieldRelation.getId(), id);
        }
        if (interfaceParamsFieldRelation !=null && "3".equals(interfaceParamsFieldRelation.getType())) {
            // 删除字段融合数据
            this.virtualUsageFieldMergeMapper.deleteByMergeObjectIds(Arrays.asList(id));
        }
    }


    @Resource
    private IProcessBranchService processBranchService;
    @Resource
    private IProcessBranchConditionService processBranchConditionService;
    @Resource
    private IProcessBranchConditionDetailService processBranchConditionDetailService;
    @Resource
    private IVirtualUsageService virtualUsageService;
    @Resource
    private IInterfaceInfoService interfaceInfoService;
    @Resource
    private IVirtualUsageGroupService virtualUsageGroupService;

    private void checkProcess(List<Long> ids, String type, boolean isBranch) {
        if (!isBranch) return;
        ProcessBranchConditionDetail processBranchConditionDetail = new ProcessBranchConditionDetail();
        if ("1".equals(type)) {
            processBranchConditionDetail.setObjectType("1");
            processBranchConditionDetail.setObjectIds(ids);
        } else {
            processBranchConditionDetail.setParamsIds(ids);
            processBranchConditionDetail.setObjectType("3");
        }
        List<ProcessBranchConditionDetail> processBranchConditionDetails = this.processBranchConditionDetailService.selectProcessBranchConditionDetailList(processBranchConditionDetail);
        if (CollectionUtil.isNotEmpty(processBranchConditionDetails)) {
            for (ProcessBranchConditionDetail branchConditionDetail : processBranchConditionDetails) {
                ProcessBranchCondition processBranchCondition = this.processBranchConditionService.selectProcessBranchConditionByBranchConditionId(branchConditionDetail.getBranchConditionId());
                if (processBranchCondition != null) {
                    InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(branchConditionDetail.getBranchInterfaceId());
                    if (interfaceInfo != null) {
                        String result = "被" + interfaceInfo.getInterfaceName() + " 分支流程引用（" + processBranchCondition.getBranchConditionName() + ")";
                        throw new BusinessException(result);
                    }
                }
            }
        }
    }

    private void checkFieldSource(List<Long> ids, String type, boolean isBranch) {
        VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
        virtualUsageParamSource.setSourceObjectIds(ids);
        virtualUsageParamSource.setSourceType(type);
        List<VirtualUsageParamSource> virtualUsageParamSourceList = this.virtualUsageParamSourceMapper.selectVirtualUsageParamSourceList(virtualUsageParamSource);
        if (CollectionUtil.isNotEmpty(virtualUsageParamSourceList)) {
            for (VirtualUsageParamSource paramSource : virtualUsageParamSourceList) {
                String result = "";
                if (paramSource.getIsInParam().equals("2")) {
                    InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(paramSource.getInterfaceId());
                    if (interfaceInfo != null) {
                        result = "被" + interfaceInfo.getInterfaceName() + "出参引用";
                    }
                } else {
                    if (paramSource.getVirtualUsageGroupId() == null) {
                        if ((isBranch && paramSource.getProcessBranchId() != null) || (!isBranch && paramSource.getProcessBranchId() == null)) {
                            VirtualUsage virtualUsage1 = new VirtualUsage();
                            virtualUsage1.setId(paramSource.getInterfaceId());
                            List<VirtualUsage> virtualUsages1 = this.virtualUsageService.selectVirtualUsages(virtualUsage1);
                            if (CollectionUtil.isNotEmpty(virtualUsages1)) {
                                result = "被" + virtualUsages1.get(0).getMasterInterfaceName() + " 下 " + virtualUsages1.get(0).getSlaveInterfaceName() + " 入参引用";
                            }
                        }
                    } else {
                        VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(paramSource.getVirtualUsageGroupId());
                        if (virtualUsageGroup != null) {
                            InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageGroup.getInterfaceId());
                            result = "被" + interfaceInfo.getInterfaceName() + "下 "+virtualUsageGroup.getGroupName()+" ";
                            VirtualUsage virtualUsage1 = new VirtualUsage();
                            virtualUsage1.setId(paramSource.getInterfaceId());
                            List<VirtualUsage> virtualUsages1 = this.virtualUsageService.selectVirtualUsages(virtualUsage1);
                            if (CollectionUtil.isNotEmpty(virtualUsages1)) {
                                result += virtualUsages1.get(0).getSlaveInterfaceName() + " 入参引用";
                            }
                        }
                    }
                }
                if (StringUtils.isNotEmpty(result)) {
                    throw new BusinessException(result);
                }
            }
        }
    }

    @Override
    public List<ParamsFieldRelation> selectParamsFieldRelationListByDataRelationId(List<Long> dataRelationIds, String dataRelationType) {
        return this.paramsFieldRelationMapper.selectParamsFieldRelationByDataRelationId(dataRelationIds, dataRelationType);
    }

    @Override
    public void saveGroupParam(BffParamReq bffParamReq, List<Long> interfaceParamsFieldRelationIds, List<Long> paramsFieldRelationIds) {
        int maxSort = this.paramsFieldRelationMapper.maxSort(bffParamReq.getInterfaceParamsFieldRelationId()) + 1;
        List<ParamsFieldRelation> paramsFieldRelationList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(paramsFieldRelationIds)) {
            ParamsFieldRelation relation = new ParamsFieldRelation();
            relation.setIds(paramsFieldRelationIds);
            List<ParamsFieldRelation> fieldRelationList = this.paramsFieldRelationMapper.selectParamsFieldRelations(relation);
            for (ParamsFieldRelation paramsFieldRelation : fieldRelationList) {
                paramsFieldRelation.setInterfaceDataListId(bffParamReq.getInterfaceParamsFieldRelationId());
                paramsFieldRelation.setIsRequiredParent(paramsFieldRelation.getIsRequired());
                paramsFieldRelation.setDataRelationId(paramsFieldRelation.getId());
                paramsFieldRelation.setDataRelationType("2");
                paramsFieldRelation.setId(null);
                paramsFieldRelationList.add(paramsFieldRelation);
            }
        }

        if (CollectionUtil.isNotEmpty(interfaceParamsFieldRelationIds)) {
            InterfaceParamsFieldRelation interfaceParamsFieldRelation = new InterfaceParamsFieldRelation();
            interfaceParamsFieldRelation.setIds(interfaceParamsFieldRelationIds);
            List<InterfaceParamsFieldRelation> interfaceParamsFieldRelationList2 = interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationList(interfaceParamsFieldRelation);
            for (InterfaceParamsFieldRelation interfaceParamsFieldRelation1 : interfaceParamsFieldRelationList2) {
                ParamsFieldRelation paramsFieldRelation = new ParamsFieldRelation();
                paramsFieldRelation.setDataRelationId(interfaceParamsFieldRelation1.getId());
                paramsFieldRelation.setIsRequiredParent(interfaceParamsFieldRelation1.getIsRequired());
                paramsFieldRelation.setFieldId(interfaceParamsFieldRelation1.getObjectId());
                paramsFieldRelation.setSortNum(maxSort ++);
                paramsFieldRelation.setDataRelationType("1");
                paramsFieldRelation.setInterfaceId(interfaceParamsFieldRelation1.getInterfaceId());
                paramsFieldRelation.setInterfaceDataListId(bffParamReq.getInterfaceParamsFieldRelationId());
                paramsFieldRelation.setAliasZhName(interfaceParamsFieldRelation1.getAliasZhName());
                paramsFieldRelation.setAliasName(interfaceParamsFieldRelation1.getAliasName());
                paramsFieldRelation.setDictCategory(interfaceParamsFieldRelation1.getDictCategory());
                paramsFieldRelation.setDictType(interfaceParamsFieldRelation1.getDictType());
                paramsFieldRelation.setDictValue(interfaceParamsFieldRelation1.getDictValue());
                paramsFieldRelationList.add(paramsFieldRelation);
            }
        }
        this.paramsFieldRelationMapper.batchSave(paramsFieldRelationList);
        InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationMapper.selectInterfaceParamsFieldRelationById(bffParamReq.getInterfaceParamsFieldRelationId());
        if (interfaceParamsFieldRelation.getIsInParam().equals("1")) {
            List<VirtualUsageParamSource> virtualUsageParamSourceList = new ArrayList<>();
            for (ParamsFieldRelation paramsFieldRelation : paramsFieldRelationList) {
                VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
                virtualUsageParamSource.setObjectId(paramsFieldRelation.getDataRelationId());
                virtualUsageParamSource.setType(paramsFieldRelation.getDataRelationType());
                virtualUsageParamSource.setSourceobjectId(paramsFieldRelation.getId());
                virtualUsageParamSource.setSourceType("2");
                virtualUsageParamSource.setInterfaceId(paramsFieldRelation.getInterfaceId());
                virtualUsageParamSource.setVirtualUsageGroupId(bffParamReq.getInterfaceId());
                int i = this.virtualUsageParamSourceMapper.update(virtualUsageParamSource);
                if (i == 0) {
                    this.virtualUsageParamSourceMapper.insertVirtualUsageParamSource(virtualUsageParamSource);
                }
            }
        }
    }
}
